package com.poorstu.manager.impl;

import java.util.ArrayList;
import java.util.List;

import com.common.base.model.PaginationSupport;
import com.common.base.model.PublicFinalData;
import com.poorstu.dao.ClassAndCollegeDao;
import com.poorstu.dao.LoginDao;
import com.poorstu.dao.RoleDao;
import com.poorstu.manager.LoginMeg;
import com.poorstu.model.ClassMod;
import com.poorstu.model.LoginMod;
import com.poorstu.model.PowerMod;
import com.poorstu.model.RoleMod;

public class LoginMegImpl implements LoginMeg{
	
	private LoginDao loginDao;
	private RoleDao roleDao;
	private ClassAndCollegeDao classAndCollegeDao;
	
	public void setLoginDao(LoginDao loginDao) {
		this.loginDao = loginDao;
	}


	public LoginMod longin(String name,String password) {
		
		 LoginMod a = new LoginMod();
		 a.setName(name);
		 a.setPassword(password);
		 
		 List<LoginMod> userlist =loginDao.select(a);
		 if(userlist.size()<=0)
		 {
			 return null;
		 }
		 if(password.equals(userlist.get(0).getPassword())) {
			 return userlist.get(0);
		}
		 return null;
		 
	}



	public List<LoginMod> selectUserAll() {
		try{
			List<LoginMod> listUser=new ArrayList<LoginMod>();
			listUser=loginDao.selectUserAll();
			return listUser;
		}catch(Exception e){
			e.printStackTrace();
			return null;}
		
	}


	public boolean menberAdd(LoginMod login,int classId,int roleId) {
		
		try{
			RoleMod role=roleDao.getRole(roleId);
			ClassMod classMod=classAndCollegeDao.selectClassById(classId);
			login.setRoleId(role);
			login.setClassId(classMod);
				
		    loginDao.menberAdd(login);
		    return true;
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}
		
		
		
	}
	
	//excel 添加
	public boolean userAdd(ArrayList<LoginMod> loginList){
		
		try{
		    loginDao.excelInput(loginList);
		    return true;
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}
	}


	public void setRoleDao(RoleDao roleDao) {
		this.roleDao = roleDao;
	}


	public void setClassAndCollegeDao(ClassAndCollegeDao classAndCollegeDao) {
		this.classAndCollegeDao = classAndCollegeDao;
	}


	public LoginMod selectUserById(int allUserId) {
		
		return loginDao.selectUserById(allUserId);
	}


	public boolean menberUpdate(LoginMod login) {
		try{
			LoginMod loginMod=new LoginMod();
			loginMod=loginDao.selectUserById(login.getId());
			loginMod.setName(login.getName());
			loginMod.setPassword(login.getPassword());
			loginMod.setUserAccount(login.getUserAccount());
			loginMod.setRoleId(login.getRoleId());
			loginMod.setClassId(login.getClassId());
			loginDao.menberUpdate(login);
			return true;
		}
		catch(Exception e){
			e.printStackTrace();
			return false;
		}
		
	}


	public boolean menberDelete(int[] user) {
		try{
		List<LoginMod> userList = new ArrayList<LoginMod>();
		for (int i = 0; i<user.length; i++) {
			LoginMod login = loginDao.selectUserById(user[i]);
			userList.add(login);
		}
			return loginDao.menberDelete(userList);
		}catch(Exception e){
			e.printStackTrace();
			return false;
			}
		}


	//登录操作
	public LoginMod login(String userAccount, String psw) {
		LoginMod user = loginDao.getUserByUserAccount(userAccount);
		if(user != null && user.getPassword().equals(psw))
		{
			LoginMod reUser = new LoginMod();
			reUser.setId(user.getId());
			reUser.setName(user.getName());
			RoleMod role = new RoleMod();
			List<PowerMod> powerList = this.roleDao.getPowerAll();
			List<PowerMod> userPowerList = new ArrayList<PowerMod>();
			for(int i = 0;i<powerList.size();i++)
			{
				if(user.getRoleId().getPowerList().contains(powerList.get(i)))
				{
					PowerMod fPowerTemp = (PowerMod)powerList.get(i);
					PowerMod fPower = new PowerMod();
					fPower.setPowerName(fPowerTemp.getPowerName());
					fPower.setPowerRoad(fPowerTemp.getPowerRoad());
					fPower.setPowerOrder(fPowerTemp.getPowerOrder());
					
					List<PowerMod> cPowerList = new ArrayList<PowerMod>();
					for(int j = 0;j<fPowerTemp.getChildPower().size();j++)
					{
						if(user.getRoleId().getPowerList().contains(fPowerTemp.getChildPower().get(j)))
						{
							PowerMod cPower = new PowerMod();
							PowerMod cPowerTemp = fPowerTemp.getChildPower().get(j);
							cPower.setPowerName(cPowerTemp.getPowerName());
							cPower.setPowerRoad(cPowerTemp.getPowerRoad());
							cPower.setPowerOrder(cPowerTemp.getPowerOrder());
							cPowerList.add(cPower);
						}
					}
					fPower.setChildPower(cPowerList);
					userPowerList.add(fPower);
				}
			}
			role.setPowerList(userPowerList);
			reUser.setRoleId(role);
			return reUser;
		}
		return null;
	}
	
	
	
	
	public boolean changePwd(int allUserId, String oldPwd, String newPwd) {
		try{
		LoginMod login=loginDao.selectUserById(allUserId);
		if(login.getPassword().equals(oldPwd))
		{
			login.setPassword(newPwd);
			loginDao.changePwd(login);
			return true;
		}
		return false;
		}catch(Exception e)
		{
			e.printStackTrace();
			return false;
		}
		
		
	}


	public PaginationSupport searchAllUserByPage(int start) {
			
			return loginDao.getUserByPage(null,null,null,start,PublicFinalData.ROLE_PAGE_MAX_SIZE);
		}


	public LoginMod selectUserByUserAccount(String userAccount) {
		
		
		return loginDao.getUserByUserAccount(userAccount);
	}


	
	
	}
    
	

